package com.cz.video.web.controller.website;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.cz.video.common.annotation.LegalValidate;
import com.cz.video.common.annotation.Log;
import com.cz.video.common.core.controller.BaseController;
import com.cz.video.common.core.domain.PageQuery;
import com.cz.video.common.core.domain.R;
import com.cz.video.common.core.page.TableDataInfo;
import com.cz.video.common.enums.BusinessType;
import com.cz.video.common.helper.LoginHelper;
import com.cz.video.website.domain.WebsiteCollectFolder;
import com.cz.video.website.domain.bo.WebsiteCollectFolderBo;
import com.cz.video.website.domain.vo.WebsiteCollectFolderVo;
import com.cz.video.website.service.IWebsiteCollectFolderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 收藏夹表 前端控制器
 * </p>
 *
 * @author yr
 * @since 2023-10-16
 */
@Tag(name="收藏夹表")
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/website/collect-folder")
public class WebsiteCollectFolderController extends BaseController {
    private final IWebsiteCollectFolderService websiteCollectFolderService;

    @Operation(summary = "添加收藏夹")
    @Log(title = "添加收藏夹", businessType = BusinessType.INSERT)
    @PostMapping("")
    @LegalValidate
    public R<Void> add(@RequestBody @Validated WebsiteCollectFolder websiteCollectFolder){
        websiteCollectFolder.setUserId(LoginHelper.getUserId());
        if (!websiteCollectFolderService.checkUserNameUnique(websiteCollectFolder)) {
            return R.fail("新增收藏夹'" + websiteCollectFolder.getName() + "'失败，收藏夹名重复了QAQ");
        }
        return toAjax(websiteCollectFolderService.insertCollectFolder(websiteCollectFolder));
    }

    @Operation(summary = "删除收藏夹")
    @Log(title = "删除收藏夹", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable Long[] id){
        return toAjax(websiteCollectFolderService.deleteById(id));
    }

    /**
     * 修改收藏夹
     */
    @SaCheckPermission("website:collectFolder:edit")
    @Operation(summary = "修改收藏夹")
    @Log(title = "收藏夹管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody WebsiteCollectFolder websiteCollectFolder) {
        return toAjax(websiteCollectFolderService.updateCollectFolder(websiteCollectFolder));
    }

    /**
     * 查询收藏夹列表
     */
    @Operation(summary = "查询收藏夹")
    @GetMapping("/selectCollectFolder")
    public TableDataInfo<WebsiteCollectFolderVo> page(WebsiteCollectFolderBo websiteCollectFolder, PageQuery pageQuery) {
        return websiteCollectFolderService.selectPageCollectFolderList(websiteCollectFolder, pageQuery);
    }
}
